//
//  UIBarButtonItem+Extension.m
//  GongDan
//
//  Created by 了凡 on 16/1/14.
//  Copyright © 2016年 河北广联. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"
//数字角标直径
#define NumMark_W_H 15

@implementation UIBarButtonItem (Extension)

+ (instancetype)BarButtonItemWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont systemFontOfSize:KScaleWidth(28)];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}


+ (instancetype)BarButtonItemWithImageName:(NSString *)imageName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

/**barItem设置图片+数量*/
+ (instancetype)BarButtonItemWithImageName:(NSString *)imageName ShowBadge:(NSInteger)badge target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 15, 20)];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
   
    UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(button.frame.size.width/2.0+6, 0, NumMark_W_H, NumMark_W_H)];
    numLabel.layer.masksToBounds = YES;
    numLabel.layer.cornerRadius = NumMark_W_H / 2;
    numLabel.text = [NSString stringWithFormat:@"%ld",badge];
    numLabel.backgroundColor = [UIColor redColor];
    numLabel.textColor = [UIColor whiteColor];
    numLabel.textAlignment = NSTextAlignmentCenter;
    numLabel.font = [UIFont systemFontOfSize:13];
    [button addSubview:numLabel];
    
    if(badge <= 0){
        //隐藏角标
        numLabel.hidden = YES;
    }else{
        //显示角标
        numLabel.hidden = NO;
    }
    
    return [[UIBarButtonItem alloc] initWithCustomView:button];
    
}
+ (instancetype)BarButtonItemWithImageName:(NSString *)imageName title:(NSString *)title target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [button setTitle:[NSString stringWithFormat:@" %@",title] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont systemFontOfSize:KScaleWidth(28)];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}
@end
